package erupt_simultaneously;

/**
 * ThreadLocal 是线程私有的，别的线程不可使用
 *
 * @Auther Jun jie Yi
 * @Date 10:32 2022/1/11
 */
public class ThreadLocalTest {

    static class ResourceClass {

        public final static ThreadLocal<String> RESOURCE_1 = new ThreadLocal<>();

        public final static ThreadLocal<String> RESOURCE_2 = new ThreadLocal<>();

    }

    static class A {

        public void setOne(String value) {
            ResourceClass.RESOURCE_1.set(value);
        }

        public void setTwo(String value) {
            ResourceClass.RESOURCE_2.set(value);
        }
    }

    static class B {
        public void display() {
            System.out.println(ResourceClass.RESOURCE_1.get() + ":" + ResourceClass.RESOURCE_2.get());
        }
    }

    public static void main(String[] args) {
        final A a = new A();
        final B b = new B();
        for (int i = 0; i < 15; i++) {
            final String resouce1 = "线程-" + i;
            final String resouce2 = " value = (" + i + ")";
            new Thread(() -> {
                try {
                    a.setOne(resouce1);
                    a.setTwo(resouce2);
                    b.display();
                } finally {
                    ResourceClass.RESOURCE_1.remove();
                    ResourceClass.RESOURCE_2.remove();
                }
            }).start();
        }
    }
}
